﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.CodeDom;
using System.Configuration;


public partial class reports_userControls_MTD_Sales : System.Web.UI.UserControl
{
    private String _TopLevel = String.Empty;
    static Int32 MTD_CNT = 0, PREV_MTD_CNT = 0, PREV_MON = 0, PACING_CNT = 0, MAX_CNT = 0;

    public String TopLevel
    {
        get { return _TopLevel; }
        set { _TopLevel = value; }
    }

    private String _RollUpUnit = String.Empty;

    public String RollUpUnit
    {
        get { return _RollUpUnit; }
        set { _RollUpUnit = value; }
    }

    private string _RollupTitle = String.Empty;

    public string RollupTitle
    {
        get { return _RollupTitle; }
        set
        {

            _RollupTitle = value.Split('(')[0];

        }
    }


    protected String CurrProdLine = String.Empty, CurrProd = String.Empty;
    protected Int32 SUB_MTD_CNT = 0, SUB_PREV_MTD_CNT = 0, SUB_PREV_MON = 0, SUB_PACING_CNT = 0;
    protected Int32 TOT_MTD_CNT = 0, TOT_PREV_MTD_CNT = 0, TOT_PREV_MON = 0, TOT_PACING_CNT = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            RollupMetadata meta = (RollupMetadata)HttpContext.Current.Session["RollupMetadata"];
            meta.ChkStacked.Checked = false;
            meta.chkShowSummary.Checked = false;

            tblCharts.Style["display"] = meta.ChkStacked.Checked || meta.chkShowSummary.Checked ? "" : "none";
            tblTables.Style["display"] = meta.chkShowTables.Checked ? "" : "none";

            string TopLevelFilter = String.Format("{1}='{0}'", RollUpUnit.Replace("'", "''"), TopLevel);

            String ManagerLevel = String.Empty;
            String RollUpFilter = String.Empty;

            switch (TopLevel)
            {
                case "Manager":
                    String[] tmpArr = RollUpUnit.Split(':');


                    string RollUpMgr = tmpArr[0];
                    ManagerLevel = tmpArr.Length == 1 ? "0" : tmpArr[1];

                    switch (ManagerLevel)
                    {
                        case "1":
                            //SQLFilter = String.Format(" B.TRAC_ID IN (SELECT TRAC_ID FROM MIS.ACC_TRAC_INFO WHERE AREA_MGR='{0}' GROUP BY 1)", RollUpFilter);
                            DisplayGrid.DataSource = (new MTDSalesByPlan()).ByAreaMgr(RollUpMgr);
                            DisplayGrid.DataBind();
                            break;

                        case "2":
                            DisplayGrid.DataSource = (new MTDSalesByPlan()).ByAcctMgr(RollUpMgr);
                            DisplayGrid.DataBind();
                            break;


                        case "0":
                            RollUpFilter = ConfigurationManager.AppSettings[RollUpMgr];
                            DisplayGrid.DataSource = (new MTDSalesByPlan()).ByRegion(RollUpFilter);
                            DisplayGrid.DataBind();
                            break;

                        default:
                            break;
                    }
                    //Heading1.Text = RollUpMgr + " ROLL UP";
                    break;

                case "State":
                    if (RollUpUnit == "ZZ")
                    {
                        RollUpFilter = ConfigurationManager.AppSettings[RollUpUnit];
                        //SQLFilter = String.Format(" B.REGION='{0}'", RollUpFilter);
                        RollupTitle = RollUpFilter;
                        DisplayGrid.DataSource = (new MTDSalesByPlan()).ByRegion(RollUpFilter);
                        DisplayGrid.DataBind();
                        break;
                    }
                    //SQLFilter = String.Format(" SUBSTR(B.TRAC_ID,1,2) = '{0}'", RollUpUnit);
                    //Heading1.Text = (String)((Hashtable)Cache["ACC_STATES"])[RollUpUnit] + " ROLL UP";
                    //Heading1.Text = RollUpUnit + " ROLL UP";
                    DisplayGrid.DataSource = (new MTDSalesByPlan()).ByState(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "Market":
                    DisplayGrid.DataSource = (new MTDSalesByPlan()).ByMarket(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "Portfolio":
                case "Portfolios":
                    DisplayGrid.DataSource = (new MTDSalesByPlan()).ByPortfolio(RollUpUnit);
                    DisplayGrid.DataBind();
                    break;

                case "TRAC_ID":
                    DisplayGrid.DataSource = (new MTDSalesByPlan()).ByTRAC_ID(RollUpUnit); ;
                    DisplayGrid.DataBind();


                    //Heading1.Text = RollUpUnit.ToUpper() + " ROLL UP";
                    break;

                default:
                    break;
            }

        }

    }

    protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DateTime startTime = DateTime.Now; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Started");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (CurrProdLine != e.Row.Cells[0].Text)
            {
                ShowSubtotalsImage();

                Table tblTemp = (Table)this.DisplayGrid.Controls[0];
                int intRow = tblTemp.Rows.GetRowIndex(e.Row);
                CurrProdLine = e.Row.Cells[0].Text;

                if (intRow > 1)
                {
                    GridViewRow gvr = new GridViewRow(intRow, intRow, DataControlRowType.Separator, DataControlRowState.Normal);
                    for (Int32 ctr = 0; ctr < 7; ctr++)
                    {
                        TableCell cellTemp = new TableCell();
                        cellTemp.Font.Bold = true;
                        cellTemp.HorizontalAlign = HorizontalAlign.Right;
                        cellTemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#9BD0FF");
                        gvr.Cells.Add(cellTemp);
                        switch (ctr)
                        {
                            case 0:
                                break;
                            case 1:
                                cellTemp.Text = "SUBTOTAL";
                                cellTemp.HorizontalAlign = HorizontalAlign.Left;
                                break;
                            case 2:
                                cellTemp.Text = SUB_MTD_CNT.ToString("n0");
                                break;
                            case 3:
                                cellTemp.Text = SUB_PREV_MTD_CNT.ToString("n0");
                                break;
                            case 4:
                                cellTemp.Text = SUB_PREV_MON.ToString("n0");
                                break;
                            case 5:
                                cellTemp.Text = SUB_PACING_CNT.ToString("n0");
                                break;
                            case 6:
                                if (SUB_PREV_MON != 0)
                                    cellTemp.Text = Convert.ToDecimal(Convert.ToDecimal(SUB_PACING_CNT) / Convert.ToDecimal(SUB_PREV_MON)).ToString("p0");
                                else
                                    cellTemp.Text = "0.0%";
                                break;
                        }
                    }

                    tblTemp.Controls.AddAt(intRow, gvr);
                }
                SUB_MTD_CNT = 0;
                SUB_PREV_MTD_CNT = 0;
                SUB_PREV_MON = 0;
                SUB_PACING_CNT = 0;
                ShowImage(e);
            }
            else
            {
                ShowImage(e);
                e.Row.Cells[0].Text = "";
            }





            SUB_MTD_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MTD"));
            SUB_PREV_MTD_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMTD"));
            SUB_PREV_MON += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMON"));
            SUB_PACING_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PACING"));

            if (CurrProd != e.Row.Cells[1].Text)
            {
                CurrProd = e.Row.Cells[1].Text;
            }
            else
            {
                e.Row.Cells[1].Text = "";
            }

            TOT_MTD_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MTD"));
            TOT_PREV_MTD_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMTD"));
            TOT_PREV_MON += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMON"));
            TOT_PACING_CNT += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PACING"));

        }

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            ShowSubtotalsImage();

            Table tblTemp = (Table)this.DisplayGrid.Controls[0];
            int intRow = tblTemp.Rows.GetRowIndex(e.Row);
            GridViewRow gvr = new GridViewRow(intRow, intRow, DataControlRowType.Separator, DataControlRowState.Normal);
            for (Int32 ctr = 0; ctr < 7; ctr++)
            {
                TableCell cellTemp = new TableCell();
                cellTemp.Font.Bold = true;
                cellTemp.HorizontalAlign = HorizontalAlign.Right;
                cellTemp.BackColor = System.Drawing.ColorTranslator.FromHtml("#9BD0FF");
                gvr.Cells.Add(cellTemp);
                switch (ctr)
                {
                    case 0:
                        break;
                    case 1:
                        cellTemp.Text = "SUBTOTAL";
                        cellTemp.HorizontalAlign = HorizontalAlign.Left;
                        break;
                    case 2:
                        cellTemp.Text = SUB_MTD_CNT.ToString("n0");
                        break;
                    case 3:
                        cellTemp.Text = SUB_PREV_MTD_CNT.ToString("n0");
                        break;
                    case 4:
                        cellTemp.Text = SUB_PREV_MON.ToString("n0");
                        break;
                    case 5:
                        cellTemp.Text = SUB_PACING_CNT.ToString("n0");
                        break;
                    case 6:
                        if (SUB_PREV_MON != 0)
                            cellTemp.Text = Convert.ToDecimal(Convert.ToDecimal(SUB_PACING_CNT) / Convert.ToDecimal(SUB_PREV_MON)).ToString("p0");
                        else
                            cellTemp.Text = "0.0%";
                        break;
                }
            }

            tblTemp.Controls.AddAt(intRow, gvr);
       
        e.Row.Cells[2].Text = TOT_MTD_CNT.ToString("n0");
        e.Row.Cells[3].Text = TOT_PREV_MTD_CNT.ToString("n0");
        e.Row.Cells[4].Text = TOT_PREV_MON.ToString("n0");
        e.Row.Cells[5].Text = TOT_PACING_CNT.ToString("n0");
        if (TOT_PREV_MON != 0)
            e.Row.Cells[6].Text = Convert.ToDecimal(Convert.ToDecimal(TOT_PACING_CNT) / (Convert.ToDecimal(TOT_PREV_MON) + Convert.ToDecimal(.01))).ToString("p0");
        else
            e.Row.Cells[6].Text = "0.0%";
        } DateTime stopTime = DateTime.Now; TimeSpan duration = stopTime - startTime; Trace.Write(System.Reflection.MethodInfo.GetCurrentMethod().Name + " Ended: " + duration.ToString());
    }

    private void ShowSubtotalsImage()
    {
        if (CurrProdLine != null && CurrProdLine.Length > 0)
        {
            GridViewRow dr = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Edit);
            TableCell dc = new TableCell(); dc.ID = "PRODUCT"; dc.Text = CurrProdLine; dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "FEATURE"; dc.Text = "SUBTOTAL"; dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "MTD"; dc.Text = SUB_MTD_CNT.ToString(); dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "PMTD"; dc.Text = SUB_PREV_MTD_CNT.ToString(); dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "PMON"; dc.Text = SUB_PREV_MON.ToString(); dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "PACING_CNT"; dc.Text = SUB_PACING_CNT.ToString(); dr.Cells.Add(dc);
            dc = new TableCell(); dc.ID = "PACE_RATE"; dc.Text = Convert.ToDecimal(Convert.ToDecimal(SUB_PACING_CNT) / (Convert.ToDecimal(SUB_PREV_MON) + Convert.ToDecimal(.01))).ToString("p0"); ; dr.Cells.Add(dc);
            GridViewRowEventArgs eSubTotals = new GridViewRowEventArgs(dr);

            ShowImage(eSubTotals);
        }
    }

    private void ShowImage(GridViewRowEventArgs e)
    {
        string sColor = string.Empty;
        Image imgChart = new Image();
        string sPlan = e.Row.Cells[1].Text.Trim().ToUpper();
        switch (e.Row.Cells[0].Text)
        {
            case "IPTV":
                sColor = "cee2fb";
                switch (sPlan.Trim().ToUpper())
                {
                    case "SUBTOTAL":
                        sColor = "aec2db";
                        imgChart = imgIPTV;
                        break;
                    case "U100":
                        imgChart = imgIPTV_U100;
                        break;
                    case "U200":
                        imgChart = imgIPTV_U200;
                        break;
                    case "U200LA":
                        imgChart = imgIPTV_U200LA;
                        break;
                    case "U300":
                        imgChart = imgIPTV_U300;
                        break;
                    case "U400":
                        imgChart = imgIPTV_U400;
                        break;
                    case "UFAM":
                        imgChart = imgIPTV_UFAM;
                        break;
                    case "U450":
                        imgChart = imgIPTV_U450;
                        break;
                    case "BP200":
                        imgChart = imgIPTV_BP200;
                        break;
                    case "UBASIC":
                        imgChart = imgIPTV_UBASIC;
                        break;
                    case "VACHLD":
                        imgChart = imgIPTV_VACHLD;
                        break;

                    default:
                        break;
                }
                break;

            case "HSIA":
                sColor = "ddf5ac";
                switch (sPlan.Trim().ToUpper())
                {
                    case "SUBTOTAL":
                        sColor = "bdd58c";
                        imgChart = imgHSIA;
                        break;
                    case "ELITE":
                            imgChart = imgHSIA_ELITE;
                        break;
                    case "EXPRES":
                        imgChart = imgHSIA_EXPRES;
                        break;
                    case "HSIA18":
                        imgChart = imgHSIA_HSIA18;
                        break;
                    case "HSIA24":
                        imgChart = imgHSIA_HSIA24;
                        break;
                    case "MAX":
                        imgChart = imgHSIA_MAX;
                        break;
                    case "PRO":
                        imgChart = imgHSIA_PRO;
                        break;
                    default: break;
                }
                break;
            case "VOIP":
                sColor = "fae5c3";
                switch (sPlan.Trim().ToUpper())
                {
                    case "SUBTOTAL":
                        sColor = "e2c5a3";
                        imgChart = imgVOIP;
                        break;
                    case "BOT250":
                        imgChart = imgVOIP_BOT250;
                        break;
                    case "UNLMTD":
                        imgChart = imgVOIP_UNLMTD;
                        break;

                    default: break;
                } break;
            default:
                break;
        }
        string PACE_RATE = "+";
        if (sPlan == "SUBTOTAL")
        {
            MTD_CNT = (int)Convert.ToDouble(e.Row.Cells[2].Text);
            PREV_MTD_CNT = (int)Convert.ToDouble(e.Row.Cells[3].Text); ;
            PREV_MON = (int)Convert.ToDouble(e.Row.Cells[4].Text); ;
            PACING_CNT = (int)Convert.ToDouble(e.Row.Cells[5].Text); ;
            PACE_RATE = e.Row.Cells[6].Text; ;
            MAX_CNT = 0;
            MAX_CNT = MAX_CNT < PREV_MON ? PREV_MON : MAX_CNT;
            MAX_CNT = MAX_CNT < PREV_MTD_CNT ? PREV_MTD_CNT : MAX_CNT;
            MAX_CNT = MAX_CNT < MTD_CNT ? MTD_CNT : MAX_CNT;
            MAX_CNT = MAX_CNT < PACING_CNT ? PACING_CNT : MAX_CNT;
            MAX_CNT = ((MAX_CNT / 100) + 1) * 100;
        }
        else
        {
            MTD_CNT = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "MTD"));
            PREV_MTD_CNT = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMTD"));
            PREV_MON = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PMON"));
            PACING_CNT = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PACING"));
            PACE_RATE = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "PACE_RATE")).ToString("p0");
        }



        if (sPlan == "SUBTOTAL")
            imgChart.ImageUrl = string.Format("http://chart.apis.google.com/chart?cht=bhg&chs=150x80&chco={0}&chbh=a&chm=N,000000,0,-1,11&chxt=y&chxl=0:|PACING|PREV+MON|PREV+MTD|MTD&chd=t:{1},{2},{3},{4}&chds=0,{5}&chtt={6}&chts={0},12", sColor, MTD_CNT, PREV_MTD_CNT, PREV_MON, PACING_CNT, MAX_CNT, PACE_RATE);
        else
            imgChart.ImageUrl = string.Format("http://chart.apis.google.com/chart?cht=bhg&chs=100x80&chco={0}&chbh=a&chm=N,000000,0,-1,11&chxt=y&chxl=0:|*|*|*|MTD&chd=t:{1},{2},{3},{4}&chds=0,{5}&chtt={6}&chts={0},12", sColor, MTD_CNT, PREV_MTD_CNT, PREV_MON, PACING_CNT, MAX_CNT, PACE_RATE);
        imgChart.Visible = true;
    }

}



